﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pathscan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        public string aspx;
        public string asp;
        public string php;
        public string jsp;
        public string jspx;//jspx路径文件
        public string backup;//备份文件
        private void Form1_Load(object sender, EventArgs e)
        {
            SetControlEnable(true);
            //加载字典文件
            //try
            //{
            //    aspx = checkBox1.Text.Trim();
            //    string aspx1 = "test.txt";//绑定文件
            //    if(aspx.Length >= 1)
            //    {
            //        OpenFileDialog dialog = new OpenFileDialog();
            //        using (StreamReader sr = new StreamReader(aspx1, Encoding.Default))
            //        {

            //        }
            //    }
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine("error" + ex);
            //}
        }
        public static void ThreadProc()
        {
            for (int i = 0; i < 100; i++)
            {
                //Console.WriteLine("ThreadProc: {0}", i);
                //// Yield the rest of the time slice.
                //Thread.Sleep(0);
            }
        }

        private void SetControlEnable(bool isEnable)
        {
            this.BeginInvoke(new Action(() => {
                button1.Enabled = isEnable;
                button2.Enabled = !isEnable;
            }));
        }

        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = "正在扫描";
            if (th == null || !th.IsAlive)
            {
                SetControlEnable(false);
                if (dataGridView1!=null && dataGridView1.Rows!=null && dataGridView1.Rows.Count > 0)
                {
                    dataGridView1.Rows.Clear();
                }
                //th = new Thread(new ThreadStart(ReadData));
                //ParameterizedThreadStart ParameterizedThreadStartDelegate = ReadData;

                //th = new Thread(new ParameterizedThreadStart(ReadData));
                //const int s1s1 = 200;
                //th.IsBackground = true;
                //th.Start(s1s1);

                th = new Thread(GetPOSJ);
                th.Start();
                //Task task = new Task(() =>
                //{
                //    ReadData();
                //});
                //task.Start();

            }
            return;

        }
        public void GetPOSJ()
        {
            string url = textBox1.Text;
            string path = "test.txt";
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                //add test
                string str = sr.ReadToEnd();
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strs = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    this.BeginInvoke(new Action(() =>
                    {
                        progressBar1.Maximum = strs.Length;
                    }));
                    for (int i = 0; i < strs.Length; i++)
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            progressBar1.Value = i + 1;
                        }));
                        try
                        {
                            System.Threading.Thread.Sleep(0);
                            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url + strs[i]);
                            HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse();
                            var v = response.StatusCode;//初始化状态
                            if (v == HttpStatusCode.OK)
                            {
                                Console.WriteLine(url + str[i], "   " + DateTime.Now + "   " + HttpStatusCode.OK);
                                dataGridView1.Rows.Add(url + strs[i], HttpStatusCode.OK);
                            }
                            label3.Text = "扫描结束";
                            httpReq.Abort();
                        }
                        catch { }
                    }
                }
            }
        }
        private void ReadData(object sender)
        {
            string url = textBox1.Text;
            string path = "test.txt";
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                //add test
                string str = sr.ReadToEnd();
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strs = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    this.BeginInvoke(new Action(() =>
                    {
                        progressBar1.Maximum = strs.Length;
                    }));
                    for (int i=0;i<strs.Length;i++)
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            progressBar1.Value = i + 1;
                        }));
                        try
                        {
                            System.Threading.Thread.Sleep(500);
                            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url + strs[i]);
                            HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse();
                            var v = response.StatusCode;//初始化状态
                            Console.WriteLine(url + v);
                            if (v == HttpStatusCode.OK)
                            {
                                Console.WriteLine(url + str[i], "   " + DateTime.Now + "   " + HttpStatusCode.OK);
                                this.BeginInvoke(new Action(() =>
                                {
                                    if (i < 36)
                                    {
                                        dataGridView1.Rows.Add(url + strs[i], HttpStatusCode.OK);
                                    }
                                  
                                }));
                            }
                            label3.Text = "扫描结束";
                            httpReq.Abort();
                        }
                        catch { }
                    }
                }


            }
            SetControlEnable(true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            SetControlEnable(true);
            if (th != null && th.IsAlive)
            {
                label3.Text = "扫描结束";
                th.Abort();
            }
            return;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value++;
            }
            else
            {
                timer1.Enabled = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //checkBox1.Checked = 
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
